package com.zchat.api.util;

import java.util.Collection;
import java.util.Properties;

import com.zchat.api.log.Logger;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

/**
 * 处理有关分辨率的类
 * @author MichaelHuang
 *
 */
public class Resolution {
	private static Resolution instance;
	static {
		instance = new Resolution();
	}
	public static Resolution getInstance() {
		return instance;
	}

	private int screenWidth = -1;
	private int screenHeight = -1;
	private int statusHeight = -1;	//the height of status
 
	/**
	 * @return the statusHeight
	 */
	public int getStatusHeight() {
		return statusHeight;
	}

	/**
	 * @param statusHeight the statusHeight to set
	 */
	public void setStatusHeight(int statusHeight) {
		this.statusHeight = statusHeight;
	}

	/**
	 * @return the screenWidth
	 */
	public int getScreenWidth() {
		return screenWidth;
	}

	/**
	 * @param screenWidth the screenWidth to set
	 */
	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}

	/**
	 * @return the screenHeight
	 */
	public int getScreenHeight() {
		return screenHeight;
	}

	/**
	 * @param screenHeight the screenHeight to set
	 */
	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}
	
	/**
	 * exchange the value of resolutions
	 */
	public void exchange() {
		this.screenWidth = this.screenWidth + this.screenHeight;
		this.screenHeight = this.screenWidth - this.screenHeight;
		this.screenWidth = this.screenWidth - this.screenHeight;
//		Logger.debug("screen width is :" + this.screenWidth + " and screen height is :" + this.screenHeight);
	}
	
	/**
	 * Verify current device is adapter now.
	 * @param config
	 * @return
	 * @author MichaelHuang
	 */
	public boolean isScreenAdapter(Properties config) {
		Collection<String> resolutions = Helpers.stringToCollection(config.get("resolution").toString().trim());
		String currentScreen = getScreenWidth() + "*" + getScreenHeight();
		if(resolutions.contains(currentScreen)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 提示用户没对应的手机分辨率
	 * @param screenWidth
	 * @param screenHeight
	 * @param context
	 * @author weixuejun
	 */
	public void promptNoResolution(final Context context){
		final String resolution = getScreenWidth() + "*" + getScreenHeight();
		
		new AlertDialog.Builder(context).setTitle("提示！").setMessage("您手机分辨率为:"+ resolution +";"+"软件暂不支持此分辨率"+"\n"+"是否将问题进行反馈？")
		.setPositiveButton("是", new AlertDialog.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
				try {
					Intent data = new Intent(Intent.ACTION_SENDTO);
					data.setType("text/plain");
					data.setData(Uri.parse("mailto:support@inforcreation.com"));
					data.putExtra(Intent.EXTRA_SUBJECT, "分辨率不兼容问题");
					data.putExtra(Intent.EXTRA_TEXT,  
							"\n\n-------------------------------------"+"\n"+"屏幕分辨率为：" + resolution + "\n"
							+ "手机型号为：" + android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL + "\n"
							+ "系统版本号为：Android OS " + android.os.Build.VERSION.RELEASE +"\n"
							+ "您所遇到的问题是:软件不支持"+resolution+"分辨率。"
							+ "欢迎您对闻道Android手机版提出宝贵意见！"+"\n"+
							"-------------------------------------"+"\n");
					context.startActivity(data);
					if(ZChatSetting.DEBUG) Logger.debug("Ready to send eamil.");
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(context, "您的手机不支持通知邮件反馈！", 300).show();
				}
			}
		})
		.setNegativeButton("否", new AlertDialog.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
				android.os.Process.killProcess(android.os.Process.myPid());
				System.exit(0);
			}
		}).setCancelable(false)
		.create().show();
	}
}